﻿using System.Collections.Generic;
using System.Linq;
using StockFinder.Model;

namespace StockFinder.Indicators.Day.ClosePrice
{
    /// <summary>
    /// Represents the stock bee trade intensity indicator which looks
    /// at the relative performance of the avergage price from last 7 days
    /// against the average price from the last 65 days
    /// </summary>
    public class ClosePriceStockBeeTradeIntensityDayIndicator : BaseDayIndicator
    {
        const int NUMBEROFPRICES = 65;

        public ClosePriceStockBeeTradeIntensityDayIndicator(int lookbackPeriod, string indicatorName)
            : base(lookbackPeriod, indicatorName)
        {
        }

        public override void ApplyIndicator(List<DailyPrice> prices)
        {
            if (!Initialize(prices)) return;

            IOrderedEnumerable<DailyPrice> orderedPrices = prices.OrderBy(p => p.PriceDate);
            DailyPrice currentPrice = null;
            int numberToSkip = 0;

            IEnumerable<DailyPrice> skippedPrices = null;
            IEnumerable<DailyPrice> takenPrices = null;

            //start from 50
            for (int i = (NUMBEROFPRICES - 1); i < orderedPrices.Count(); i++)
            {
                currentPrice = orderedPrices.ElementAt(i);

                numberToSkip = (i + 1) - NUMBEROFPRICES;

                skippedPrices = orderedPrices.Skip(numberToSkip);

                takenPrices = skippedPrices.Take(NUMBEROFPRICES);

                var average65 = takenPrices.Average(p => p.AdjustedClose);

                var lastWeek = takenPrices.Reverse().Take(7);

                //currentPrice.StockBeeTradeIntensity7_65 = (lastWeek.Average(p => p.AdjustedClose) / average65) * 100;
            }
        }
    }
}
